/*-------------------------------------------------------------------------------
* file:     app_lowpower.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* include ----------------------------------------------------------------*/
#include "app_parameter.h"
#include "app_lowpower.h"
#include "wn8012_ck801.h"
#include "lpt.h"
#include "app_lpt.h"
/* externs variablesr------------------------------------------------------*/

/* externs function--------------------------------------------------------*/

/*Not Used Private variablesr----------------------------------------------*/
#if NO_USEAD
volatile uint16_t no_key_press_timer;       //用来禁用触摸板（暂时为未用到）
volatile uint8_t delay_send_gsensor_tick;   // wn8012进入低功耗时，禁止G-Sensor数据一段时间
volatile uint8_t delay_send_key_tick;       // wn8012进入低功耗时，禁止按键数据一段时间
#endif

/* Private variablesr------------------------------------------------------*/
volatile uint16_t lowpower_timer_tick;      //低功耗计时 100ms
volatile uint16_t lowpower_sleep_timer_tick;      //低功耗计休眠时 20ms
volatile uint8_t f_entry_sleep;            //使能进入低功耗模式
volatile uint8_t delay_sleep_tick;          //用来收到休眠指令时，延时休眠时间

/*------------------------------------------------------------------------*/
volatile uint32_t close_wdt_flag;          //用来关闭看门狗
volatile uint8_t delay_send_wake_info;      // 延时发送唤醒包
volatile uint8_t delay_send_wake_flag;      // 延时发送标志
volatile uint16_t delay_key_done_disable_tp_tick; //按键抬起禁用触摸板0.4s计时
volatile uint16_t delay_key_done_2_s_disable_tp_tick;

volatile bool global_15s_tick_weakup_flag; //15s唤醒标志
volatile bool global_15s_sleep_exit_flag; 
volatile uint8_t global_sleep_timer_clear_flag; 

volatile bool global_weakup_flag; //唤醒标志
volatile uint32_t global_weakup_tick; 

extern volatile uint16_t global_tp_alg_tick;
extern volatile bool global_tp_alg_botton;

extern uint8_t app_stop_wait_flag;
		
/*
 * @brief : 休眠唤醒包标志
 *
 * 1: sleep ; 0: wakeup
 */
uint8_t one_wire_bus_app_sleep_or_wakeup_flag = 0;

/* Private function--------------------------------------------------------*/
/**
 * @brief 低功耗初始化
 * 
 */
void app_lowpower_init(void)
{ 
    //no_key_press_timer = 0;
    lowpower_sleep_timer_tick = 0;
    lowpower_timer_tick = 0;
    f_entry_sleep = 0;
    close_wdt_flag = 0x00;

    delay_send_wake_info = 0x00; // init
    delay_send_wake_flag = 0x00; // init

    delay_key_done_disable_tp_tick = 0x00;
    delay_key_done_2_s_disable_tp_tick = 0x00;

    global_15s_tick_weakup_flag = false;
    global_15s_sleep_exit_flag = false;
    
    global_sleep_timer_clear_flag = 0;
	global_weakup_flag=false;
	global_weakup_tick=0;
	
	golbal_sleep_15_s_in_sleep_flag=false;
	global_sleep_15_s_in_sleep_tick=0;
}

/**
 * @brief ：定时器调用的低功耗相关函数,0.1mS调一次
 * 
 */
void app_lowpower_timer_tick_0_1ms_callback(void)
{   
    lowpower_timer_tick++;
    lowpower_sleep_timer_tick ++;

    if(lowpower_timer_tick >= 100*10) //100 * 1ms
    {
        lowpower_timer_tick = 0;
        if(delay_key_done_disable_tp_tick > 0) //delay_key_done_disable_tp_tick = 4
        {
            delay_key_done_disable_tp_tick --;
        }
        if(delay_key_done_2_s_disable_tp_tick > 0) //delay_key_done_2_s_disable_tp_tick = 10
        {
            delay_key_done_2_s_disable_tp_tick --;
        }
        if(touch_tick > 0)
        {
            touch_tick --;
            
        }
    }
//	if(global_weakup_flag)
//	{
//		global_weakup_tick=0;
//	}else{
//		global_weakup_tick++;
//		if(global_weakup_tick>2000)
//		{
//			global_weakup_flag=true;
//		}
//	}

    if(lowpower_sleep_timer_tick >= 10)
    {
        lowpower_sleep_timer_tick = 0;
        if(delay_sleep_tick > 0)  
        {
            //有休眠指令进入次判断(100ms进入休眠，可以去掉)
            delay_sleep_tick --;
            if(delay_sleep_tick == 0)
            {
                f_entry_sleep = 1;
            }
        }
        if(global_tp_alg_tick > 0)
        {
            global_tp_alg_tick --;
            if(global_tp_alg_tick == 0)
            {
                global_tp_alg_botton = true;
            }
        }
    }
    
}

//----------------------------------------------------------------
/**
 * @brief 设置tp禁用时间
 * 
 */
void app_lowpower_disable_tp_set(void)
{
    delay_key_done_2_s_disable_tp_tick = 20;
	first_touch_flag_f1 = 1;
	first_touch_flag_f2 = 1;
	touch_idle_flag=1;

    if(Waiting_sleep)
    {
        // Waiting_sleep = 0x00;
        //在休眠指令收到之后按键完全抬起
        one_wire_bus_app_sleep_or_wakeup_flag = 1;
        app_lowpower_sleep_set();
    }
    
}

/**
 * @brief 设置禁用标志
 */
void app_lowpower_set_in_sleep_flag(void)
{
    uint32_t psr;

    psr = csi_irq_save();
    f_entry_sleep = 1;
    csi_irq_restore(psr);
}

/**
 * @brief 设置tp禁用时间
 * 
 */
//void app_lowpower_key_up__disable_tp(void)
//{
//    delay_key_done_disable_tp_tick = 4;
//}
//---------------------------------------------------------------
/**
 * @brief ：设置键盘进入正常休眠
 * 
 */
void app_lowpower_sleep_set(void)
{
    uint32_t psr;
    psr = csi_irq_save();
    golbal_keyboard_15_s_in_sleep_flag = false;
    delay_sleep_tick = 50;//最大2*100ms 延迟
	f_entry_sleep = 0;
    csi_irq_restore(psr);
}

/**
 * @brief ：设置键盘退出休眠
 * 
 */
void app_lowpower_sleep_clear(void)
{
    delay_sleep_tick = 0;
}

/**
 * @brief 控制发送唤醒包
 * 
 */
void app_send_wake_info_delay(void)
{ 
    delay_send_wake_info = 1;
}

void app_sleep_timer_clear(void)
{
    global_sleep_timer_clear_flag = 1;
	golbal_sleep_15_s_in_sleep_flag=false;
	global_sleep_15_s_in_sleep_tick=0;
}

/**
 * @brief 关闭看门狗
 * 
 * @param wdt_flag 
 */
void app_lowpower_close_wdt(uint32_t wdt_flag)
{
    if(wdt_flag == close_wdt_flag)
    {
        if(wdt_flag == CLOSE_SYS_WDT_FLAG)
        {
            csi_iwdt_close();
        }
    }
    close_wdt_flag = 0x00;
}

/**
 * @brief : 低功耗处理进程
 *          SYSCFG->BKD ： 深休眠时可保存数据的寄存器
 * @param 
 * @return 
 */
 #if 0
void app_lowpower_process(void)
{
    if(one_wire_bus_app_sleep_or_wakeup_flag && f_entry_sleep)
    {
        
        #if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("Zzz\r\n");
        #endif
        //timer sleep`
        // app_timer_sleep_and_weakup_set(false);

        #if (APP_LOG_ALL_ENABLE == 0)
            app_hardware_ver_lowpower_io_set(false);
        #endif
        app_key_low_power_sleep_set(false);    //键盘io

       // app_touch_pad_lowpower_io_set(false); //iic和tp
        app_gsensor_sleep_io_set(false);        //Gsensor
        app_uart_sleep_and_weakup_io_set(false);//uart

		lowpower_sleep_timer_tick = 0;
		lowpower_timer_tick = 0;
        close_wdt_flag = CLOSE_SYS_WDT_FLAG;
        app_lowpower_close_wdt(CLOSE_SYS_WDT_FLAG);

        if(golbal_keyboard_15_s_in_sleep_flag)
        {
            app_iwt_clr();
            global_15s_tick_weakup_flag = false;
            global_15s_sleep_exit_flag = true;
            Lpt_Config();
            udelay(500);
        }else
        {
            global_15s_sleep_exit_flag = false;
        }
        csi_pm_enter_sleep(PM_MODE_DEEPSLEEP);   //进入深度睡眠
#if(WDT_DISABLE == 0)
    csi_iwdt_open();
#endif
        #if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("exit Zzz\r\n");
        #endif
        if(golbal_keyboard_15_s_in_sleep_flag)
        {
            //15s定时器唤醒
            LPT_Stop();
            app_iwt_clr();
        }
        if(!global_15s_tick_weakup_flag)
        {
            //不是定时器唤醒时，不执行500ms操作，执行常规唤醒操作
            global_500ms_weakup_process_flag = false;
            //退出15s机内休眠标志
            golbal_keyboard_15_s_in_sleep_flag = false;
        }else{
			//定时器唤醒
			global_500ms_weakup_process_flag = true;
		}
        
        #if (APP_LOG_ALL_ENABLE == 0)
            app_hardware_ver_lowpower_io_set(true);
        #endif
        app_key_low_power_sleep_set(true);
        //app_touch_pad_lowpower_io_set(true); //iic和tp
        app_gsensor_sleep_io_set(true);
        app_uart_sleep_and_weakup_io_set(true);

        lowpower_timer_tick = 0;
        f_entry_sleep = 0;
        global_upgrade_stop_herat_flag = 0x01;
    }
    if((one_wire_bus_app_sleep_or_wakeup_flag == 0)&&(delay_send_wake_info))
    {
        delay_sleep_tick  = 0;
        delay_send_wake_info = 0;
        if(!Waiting_sleep) 
        {
            //收到休眠指令时没有按键按下
//            one_wire_bus_phy_adjust_tx_timer1();        //设置上报时间时基
//            one_wire_bus_app_tp_updata_timer_tick(40);
//            one_wrie_bus_app_send_weakup_beat(0x01);
        }
        #if FLASH_LOG_PRINTF_FLAG
            app_flash_log_printf(":weakUP",NULL,0);
        #endif
    }
    if((one_wire_bus_app_sleep_or_wakeup_flag == 0)&&(global_sleep_timer_clear_flag))
    {
        global_sleep_timer_clear_flag = 0;
        global_keyboard_15_s_in_sleep_tick = 0x00;
    }
}
#else
void app_lowpower_process(void)
{
	if(ota_first_flag)
	{
		one_wire_bus_app_sleep_or_wakeup_flag=0;
		global_sleep_15_s_in_sleep_tick=0;
		return;
	}
		
	if(one_wire_bus_app_sleep_or_wakeup_flag)
	{
		global_weakup_flag=false;
	}
		
    if(one_wire_bus_app_sleep_or_wakeup_flag && f_entry_sleep)
    {
        
        #if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("Zzz\r\n");
        #endif
        //timer sleep`
        // app_timer_sleep_and_weakup_set(false);

        #if (APP_LOG_ALL_ENABLE == 0)
//            app_hardware_ver_lowpower_io_set(false);
        #endif
        app_key_low_power_sleep_set(false);
        app_gsensor_sleep_io_set(false);
//        app_touch_pad_lowpower_io_set(false); //iic和tp

        app_uart_sleep_and_weakup_io_set(false);

		lowpower_sleep_timer_tick = 0;
		lowpower_timer_tick = 0;
        close_wdt_flag = CLOSE_SYS_WDT_FLAG;
        app_lowpower_close_wdt(CLOSE_SYS_WDT_FLAG);

//        if(golbal_keyboard_15_s_in_sleep_flag)
//        {
//            app_iwt_clr();
//            global_15s_tick_weakup_flag = false;
//            global_15s_sleep_exit_flag = true;
//            Lpt_Config();
//            udelay(500);
//        }else
//        {
//            global_15s_sleep_exit_flag = false;
//        }
        csi_pm_enter_sleep(PM_MODE_DEEPSLEEP);
#if(WDT_DISABLE == 0)
    csi_iwdt_open();
#endif
        #if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("exit Zzz\r\n");
        #endif
//        if(golbal_keyboard_15_s_in_sleep_flag)
//        {
//            //15s定时器唤醒
//            LPT_Stop();
//            app_iwt_clr();
//        }
//        if(!global_15s_tick_weakup_flag)
//        {
//            //不是定时器唤醒时，不执行500ms操作，执行常规唤醒操作
//            global_500ms_weakup_process_flag = false;
//            //退出15s机内休眠标志
//            golbal_keyboard_15_s_in_sleep_flag = false;
//        }else{
//			//定时器唤醒
//			global_500ms_weakup_process_flag = true;
//		}
        
        #if (APP_LOG_ALL_ENABLE == 0)
//            app_hardware_ver_lowpower_io_set(true);
        #endif
        app_key_low_power_sleep_set(true);
//        app_touch_pad_lowpower_io_set(true); //iic和tp
        app_gsensor_sleep_io_set(true);
        app_uart_sleep_and_weakup_io_set(true);


		
        lowpower_timer_tick = 0;
        f_entry_sleep = 0;
		
        global_upgrade_stop_herat_flag = 0x01;
		one_wire_bus_app_sleep_or_wakeup_flag=0;
		global_gsensor_enable = true;
		flash_log_flag=0;
		flash_log_tiner_tick=0;
		if(!app_stop_wait_flag)//按键唤醒
		{
			global_weakup_flag=true;
			global_weakup_tick=0;
			udelay(2000);
			one_wrie_bus_app_awaken_cmd();
			kb_lock_state_flag = kb_lock_state;
			kb_lock_state=1;
			rec_single_evt_test(MaintenanceServiceID,REMOTE_MCU_STATUS,0x01,0x01,0);
		}else{
			app_stop_wait_flag=0;			
			rec_single_evt_test(MaintenanceServiceID,REMOTE_MCU_STATUS,0x01,0x01,2);
		}
		
		get_kb_lock_flag=0;
    }

//    if((one_wire_bus_app_sleep_or_wakeup_flag == 0)&&(global_sleep_timer_clear_flag))
//    {
//        global_sleep_timer_clear_flag = 0;
//        global_keyboard_15_s_in_sleep_tick = 0x00;
//		
//		golbal_sleep_15_s_in_sleep_flag=false;
//		global_sleep_15_s_in_sleep_tick=0;
//    }
	
//	if(golbal_sleep_15_s_in_sleep_flag && global_key_power_on_flag)
	if(golbal_sleep_15_s_in_sleep_flag &&!factory_enter_flag )
	{
		if(global_key_power_on_flag < 1 || !pad_lcd_state)    //aaa
        return;
		#if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("doze\r\n");
        #endif
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_SLEEP_STA,0x01,0x01,1);
		app_key_low_power_sleep_set(false);
		app_timer_sleep_and_weakup_set(false);
        //app_touch_pad_doze_io_set(false); //iic和tp
        app_gsensor_sleep_io_set(false);
        app_uart_doze_and_weakup_io_set(false);
		app_stop_wait_flag = 0;
		lowpower_sleep_timer_tick = 0;
		lowpower_timer_tick = 0;
        close_wdt_flag = CLOSE_SYS_WDT_FLAG;
        app_lowpower_close_wdt(CLOSE_SYS_WDT_FLAG);

		app_lpt_init(500);
		app_sleep_heart_beat();
#if(WDT_DISABLE == 0)
    csi_iwdt_open();
#endif
		app_timer_sleep_and_weakup_set(true);
		app_key_low_power_sleep_set(true);
        //app_touch_pad_doze_io_set(true); //iic和tp
        app_gsensor_sleep_io_set(true);
        app_uart_sleep_and_weakup_io_set(true);
		golbal_sleep_15_s_in_sleep_flag=false;
		global_sleep_15_s_in_sleep_tick=0;
		
		flash_log_flag=0;
		flash_log_tiner_tick=0;
		if(app_stop_wait_flag)
		{
			app_stop_wait_flag=0;
		}
		#if APP_DEBUG_POWER_ON_LOG_ENABLE
            console_log_string("exit doze\r\n");
        #endif
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_SLEEP_STA,0x01,0x01,0);
	}
}
#endif


